﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReAnimation.MVP.ViewInterfaces;
using GLWrap;
using ReAnimation.Graphics.Handlers;
using ReAnimation.MVP.Presenters;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.Infrastructure.Services.Interfaces;
using ReAnimation.Helpers.IoC;
using ReAnimation.Helpers.Common;
using System.IO;
using System.Diagnostics;

namespace ReAnimation.MVP.Views
{
    public partial class SceneManipulationView : BaseUserControlView, ISceneManipulationView
    {
        IApplicationSettings _applicationSettings;
        ILazyInject<ISaveFileDialogView> _saveFileDialogInj;
        IApplicationSettingsView _applicationSettingsView;
        IAnimationToPOVService _animationToPovService;
        IGlSceneManager _sceneManager;
        IMainModel _mainModel;
        IMoleculesDisplayModel _moleculesDisplayModel;
        SceneManipulationPresenter _presenter;
        NavigationHandler _navigationHandler;
        MoleculeTransformHandler _moleculeTransformHandler;

        public SceneManipulationView(IGlSceneManager sceneManager,
            IApplicationSettings applicationSettings,
            ILazyInject<ISaveFileDialogView> saveFileDialogInj,
            IAnimationToPOVService animationToPovService,
            IApplicationSettingsView applicationSettingsView,
                                     IMoleculesDisplayModel moleculesDisplayModel,
                                     IMainModel mainModel,
                                     SceneManipulationPresenter presenter)
        {
            _applicationSettings = applicationSettings;
            _saveFileDialogInj = saveFileDialogInj;
            _animationToPovService = animationToPovService;
            _applicationSettingsView = applicationSettingsView;
            _presenter = presenter;
            _sceneManager = sceneManager;
            _moleculesDisplayModel = moleculesDisplayModel;
            _mainModel = mainModel;
            _navigationHandler = new NavigationHandler(sceneManager);
            _moleculeTransformHandler = new MoleculeTransformHandler();

            InitializeComponent();

            _mainModel.CurrentMoleculeChanged += (s, o) =>
                {
                    _moleculeTransformHandler.StopHandle();
                    _navigationHandler.StopHandle();
                };

            _presenter.View = this;
        }

        public bool CameraNavigationEnabled 
        {
            set
            {
                cameraNavigationButton.Enabled = value;
            }
        }

        private void bottomFlowLayout_Paint(object sender, PaintEventArgs e)
        {

        }


      
        private void rotateMoleculeButton_Load(object sender, EventArgs e)
        {

        }

        private void rotateMoleculeButton_Click_1(object sender, EventArgs e)
        {
            if (_mainModel.CurrentMolecule != null)
            {
                if (_moleculesDisplayModel.MoleculeDrawers.ContainsKey(_mainModel.CurrentMolecule))
                {
                    if (_moleculeTransformHandler.IsActive)
                        _moleculeTransformHandler.StopHandle();
                    else
                    {
                        _moleculeTransformHandler.MoleculeDrawer = _moleculesDisplayModel.MoleculeDrawers[_mainModel.CurrentMolecule];
                        _moleculeTransformHandler.StartHandle();
                    }
                }
            }
        }

        private void cameraNavigationButton_Click_1(object sender, EventArgs e)
        {
            _moleculeTransformHandler.StopHandle();
            if (_navigationHandler.IsActive)
                _navigationHandler.StopHandle();
            else
                _navigationHandler.StartHandle();
        }

        private void Settings_Click(object sender, EventArgs e)
        {
            _applicationSettingsView.ShowModal();
        }

        private void snapshotBtn_Click(object sender, EventArgs e)
        {
            if(_mainModel.CurrentAnimationStep == null) return;
            ISaveFileDialogView saveFileDialog = _saveFileDialogInj.Get();
            
            if (saveFileDialog.ShowOpenFileDialog())
            {
                String fileName = saveFileDialog.FileName;
                String dir = Path.GetDirectoryName(fileName);
                String povFileName = "{0}.pov".F((new Random()).Next());
                FileStream fs = new FileStream("{0}/{1}".F(dir,povFileName), FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);

                _animationToPovService.ConvertAnimationStepToPOV(_mainModel.CurrentAnimationStep, sw);

                sw.Flush();
                sw.Close();
                fs.Close();
                Process p = Process.Start(_applicationSettings.PovRayPath, _applicationSettings.PovRayParams + " /RENDER /EXIT +W640 +H480 +O\"{0}\" \"{1}/{2}\"".F(fileName,dir,povFileName));
                p.WaitForExit();

                File.Delete("{0}/{1}".F(dir, povFileName));
            }
            
        }
    }
}
